package com.auto.learning.net;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.auto.learning.R;
import com.auto.learning.activity.MyBaseInfo;
import com.auto.learning.bean.AnchorBean;
import com.auto.learning.bean.AnchorBookBean;
import com.auto.learning.bean.BannerItem;
import com.auto.learning.bean.BookBean;
import com.auto.learning.bean.CacheTypeBean;
import com.auto.learning.bean.ListenStatusBean;
import com.auto.learning.bean.ResponeResult;
import com.auto.learning.bean.UserInfoBean;
import com.auto.learning.event.DownLoadRefreshEvent;
import com.auto.learning.event.EventBus;
import com.auto.learning.event.Handle;
import com.auto.learning.service.MyUserInfo;
import com.auto.learning.store.core.StaticReference;
import com.auto.learning.thread.pool.Pool;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.SharedPreferencesUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class HttpRequestHelper {
	public static final boolean M_SHOW_LOG = false; // 是否显示网络请求日志

	// public static Map<Integer, ArrayList<BookBean>> bookListMap = new HashMap<Integer, ArrayList<BookBean>>();
	public static ArrayList<BannerItem> bannerItemList = new ArrayList<BannerItem>();
	public static ArrayList<BookBean> bookList = new ArrayList<BookBean>();
	public static ArrayList<BookBean> newList = new ArrayList<BookBean>();
	public static ArrayList<BookBean> topList = new ArrayList<BookBean>();

	// 接口
	public final static String USER_BANNER = "/user-banner";
	public final static String BANNDE_LIST = "/banner-list";
	public final static String GUESS_YOU_LIKE = "/guess-you-like";
	public final static String ANCHOR_BOOK_LIST = "/anchor-book-list";
	public final static String LISTEN_MODE = "/listen-mode";
	public final static String READ_MODE = "/read-mode";
	public final static String HALL_OF_FAME = "/hall-of-fame";
	public final static String NATION_TYPE_LIST = "/nation-type-list";
	public final static String SEARCH = "/search";
	public final static String BOOK_COMMENT_LIST = "/book-comment-list";
	public final static String BOOK_COMMENT_FAVOR = "/book-comment-favour";
	public final static String BOOK_COMMENT_ADD = "/book-comment-add";
	public final static String BOOK_COMMENT_DELETE = "/book-comment-del";
	public final static String BOOK_COLLECT_ADD = "/book-collect";
	public final static String MY_FAVOUR_BOOK = "/my-favour-book";
	public final static String CANCEL_COLLECT_BOOK = "/book-collect-cancel";
	public final static String BOOK_DOWNLOAD = "/book-download";
	public final static String BOOK_DOWNLOAD_DEL = "/book-download-del";
	public final static String BOOK_LISTEN = "/book-listen";
	public final static String BOOK_READ = "/book-read";
	public final static String MY_READ_BOOK = "/my-read-book";
	public final static String LOGIN = "/login";
	public final static String CODE_LOGIN = "/code-login";
	public final static String CODE = "/code";
	public final static String CHECK_CODE = "/check-code";
	public final static String USER_INFO = "/user-info";
	public final static String wexin_login = "/weixin-login";
	public final static String qq_login = "/qq-login";
	public final static String LOG_OUT = "/logout";
	public final static String NICK_NAME_CHANGE = "/nick-name-change";
	public final static String USER_READ_INFO = "/user-read-info";
	public final static String USER_RANK_LIST = "/user-rank-list";
	public final static String CHANGE_PWD = "/change-pwd";
	public final static String MY_GRADE_INFO = "/my-grade-info";
	public final static String FORGET_PWD = "/forget-pwd";
	public final static String REGISTER = "/register";
	public final static String BOOK_ANCHOR_LIST = "/book-anchor-list";
	public final static String USER_BIND_WEIXIN = "/user-bind-weixin";
	public final static String USER_BIND_QQ = "/user-bind-qq";
	public final static String USER_SHARE_ADD = "/user-share-add";
	public final static String TEL_BIND_CHECK = "/tel-bind-check";
	public final static String USER_BIND_TEL = "/user-bind-tel";
	public final static String HOF_LOOK = "/hof-look";
	public final static String BOOT_SCREEN = "/boot-screen";
	public final static String ANCHOR_FAVOUR = "/anchor-favour";
	public final static String INVITE_CODE = "/invite-code";
	public final static String INVITE_CODE_CANCEL = "/invite-code-cancel";
	public final static String SEARCH_ALL = "/search-all";
	public final static String SEARCH_ALL_HOF = "/search-all-hof";
	public final static String SEARCH_ALL_BOOK = "/search-all-book";
	public final static String SEARCH_ALL_ANCHOR = "/search-all-anchor";
	public final static String USER_LIKE = "/user-like";
	public final static String NEWEST_BOOK = "/newest-book";
	public final static String WEEK_RANK = "/week-rank";
	public final static String MONTH_RANK = "/month-rank";
	public final static String COMMENT_SUBMIT = "/comment-submit";
	public final static String BOOK_REVIEW_LIST = "/book-review-list";
	public final static String COMMENT_FAVOUR = "/comment-favour";
	public final static String COMMENT_FAVOUR_DEL = "/comment-favour-del";
	public final static String COMMENT_REPLY_ADD = "/comment-reply-add";
	public final static String COMMENT_REPLY_LIST = "/comment-reply-list";
	public final static String COMMENT_REPLY_DEL = "/comment-reply-del";
	public final static String REMIND_LIST = "/remind-list";
	public final static String REMIND_DEL = "/remind-del";
	public final static String REMIND_REPLY = "/remind-reply";
	public final static String NOTIFY_LIST = "/notify-list";
	public final static String NOTIFY_DEL = "/notify-del";
	public final static String BOOK_REVIEW_INFO = "/book-review-info";
	public final static String COMMENT_WARN = "/comment-warn";
	public final static String BOOK_LIST_DETAIL = "/book-list-detail";
	public final static String BOOK_LIST_INFO = "/book-list-info";
	public final static String SEARCH_HOT = "/search-hot";
	public final static String BOOK_TYPE_SORT = "/book-type-sort";
	public final static String PAY_BUY_BOOK = "/pay/buy-book";
	public final static String PAY_BUY_YEAR = "/pay/buy-year";
	public final static String PAY_BUY_RECHARGE = "/pay/recharge";
	public final static String PAY_BUY_YEAR_SUM = "/pay/buy-year-sum";
	public final static String PAY_BUY_BOOK_LIST = "/pay/buy-booklist";

	// 常量
	public final static String PAGE_INDEX = "pageIndex";
	public final static String PAGE_SIZE = "pageSize";
	public final static String IMSI_KEY = "uuid";
	public final static String USERID = "userId";
	public final static String BOOKID = "bookId";
	public final static String SECTIONID = "sectionId";
	public final static String BOOKIDS = "bookIds";
	public final static String TYPEID = "typeId";
	public final static String COMMENTID = "commentId";
	public final static String COMMENT = "comment";
	public final static String COLLECT_EVENT = "s_collect_success"; // 收藏成功全局改变状态
	public final static String CONTINUE_PLAY_BOOKID = "continuePlay_bookId"; // 断点的书的Id
	public final static String CONTINUE_PLAY_CHAPTERID = "continuePlay_chapterId"; // 断点的章节Id
	public final static String CONTINUE_PLAY_BOOK_TIME = "continuePlay_time"; // 断点时间
	public final static String CONTINUE_PLAY_BOOK_TOTAL_TIME = "continuePlay_time_total"; // 断点书的总时间
	public final static String CONTINUE_PLAY_BEFORE = "continue_play_before"; // 偏好设置--继续上次收听
	public final static String DWONLOAD_PLAY_CONTINUE = "download_play_continue"; // 偏好设置--  2、3、4G播放和下载
	public final static String BOOKNUM_ISCHANGED = "bookNumIsChanged"; // 首页显示的是否为排行榜，1否2是
	public final static String M_TILE_OFF = "m_timeOff"; // 播放定时关闭
	public final static String M_TILE_VALUE = "m_timeValue"; // 播放定时关闭时间，单位（分）
	public final static String M_TILE_POSITION = "m_timeposition"; // 播放定时关闭时间，单位（分）
	public final static String M_TILE_WNEN_CLOSE = "m_timeWhenClose"; // 播放定时关闭时间，单位（分）

	public final static String M_HAS_SHOW_HOME_RIGHT = "m_has_show_home_right"; // 首页显示tips
	public final static String M_HAS_SHOW_HOME_REFRESH_DOWN = "m_has_show_home_refresh_down"; // 首页显示tips--下来刷新

	public final static String ALARM_ACTION = "android.auto.learning.alarm.action"; // 闹钟
	public static final String SP_UUID_KEY = "SP_UUID_KEY";
	public static final String SP_UUID = "SP_UUID";

	/**
	 * 滴滴打车
	 */
	public final static String DIDI_APPID = "didi64584B504F48782F67323673536341";
	public final static String DIDI_SCERET = "f2546e23a9b58a30c9c6d48ced399e70";

	/**
	 * 全局变量
	 */
	public final static int COLLECT_SUCCESS = -500;
	public final static int CANCEL_COLLECT_SUCCESS = -501;
	public final static int COLLECT_FAIL = -502;
	public final static int GETDATA_SUCCESS = 0x11; // 网络请求成功后的操作
	public final static int GETDATA_FAIL = 0x14; // 网络请求失败
	public final static int PUBLIC_LOGIN_SUCCESS = 0x12; // 登录成功
	public final static int PUBLIC_LOGIN_FAIL = 0x13; // 登录失败
	public final static int GET_LOCAL_BOOKBEAN_SUCCESS = -1; // 获取本地缓存的数据成功

	public final static int BLUR_DEFAULT_TIMES = 3;

	public final static int SEND_LISTEN_STATUS_SUCCESS = -9999;
	public final static int SEND_LISTEN_STATUS_FAIL = -9998;
	public final static int SEND_READ_STATUS_SUCCESS = -9997;
	public final static int SEND_READ_STATUS_FAIL = -9996;

	public static final int MAX_IMAGE_SIZE = 9;
	
	/**
	 * 买书
	 */
	public final static int BUY_BOOK_SUCCESS = 0x778;
	public final static int BUY_BOOK_FAIL = 0x779;

	/**
	 * 获取网络数据
	 * 
	 * @param _context
	 * @param _url
	 * @param _params
	 * @param _handle
	 * @param _success
	 * 
	 */
	public static void executeHttpData(final Context _context,
			final String _url, final List<NameValuePair> _params,
			final Handle _handle, final int _success) {
		Pool.getPool().execute(new Runnable() {
			@Override
			public void run() {
				if (HttpRequestHelper.getUserId(_context)) {
					ResponeResult responeResult = new ResponeResult();
					try {
						HttpEntity entity = HttpHelper.getInfoByPost(_url, _params, _context);
						String _result = HttpHelper.entity2String(entity);
						JSONObject jsonResult = new JSONObject(_result);
						JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
						int code = result.getInt(HttpCommonInfo.RESULT_CODE);
						String msg = result.getString(HttpCommonInfo.RESULT_MSG);
						responeResult.setCode(code);
						responeResult.setMsg(msg);
						if (code == 0) {
							responeResult.setData(jsonResult.getString(HttpCommonInfo.DATA));
							_handle.obtainMessage(_success, responeResult).sendToTarget();
						} else {
							_handle.obtainMessage(-100, msg).sendToTarget();
						}
					} catch (ClientProtocolException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (IOException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (JSONException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (Exception e) {
						e.printStackTrace();
						responeResult = null;
					}
				}
			}
		});
	}

	/**
	 * 获取网络数据
	 * 
	 * @param _context
	 * @param _url
	 * @param _params
	 * @param _handle
	 * @param _success
	 * 
	 */
	public static void executeHttpData(final Context _context, final String _url, final List<NameValuePair> _params, final Handle _handle, final int _success, final int _fail) {
		Pool.getPool().execute(new Runnable() {
			@Override
			public void run() {
				String err = "";
				if (HttpRequestHelper.getUserId(_context)) {

					ResponeResult responeResult = new ResponeResult();
					try {
						HttpEntity entity = HttpHelper.getInfoByPost(_url, _params, _context);
						String _result = HttpHelper.entity2String(entity);
						if (_result != null && !"".equals(_result)) {
							JSONObject jsonResult = new JSONObject(_result);
							JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
							int code = result.getInt(HttpCommonInfo.RESULT_CODE);
							String msg = result.getString(HttpCommonInfo.RESULT_MSG);
							responeResult.setCode(code);
							responeResult.setMsg(msg);
							if (code == 0) {
								responeResult.setData(jsonResult.getString(HttpCommonInfo.DATA));
								_handle.obtainMessage(_success, responeResult).sendToTarget();
							} else {
								_handle.obtainMessage(_fail, msg).sendToTarget();
							}
						}
						
					} catch (ClientProtocolException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (IOException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (JSONException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (NullPointerException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} finally {
						if (!TextUtils.isEmpty(err)) {
							_handle.obtainMessage(_fail, err).sendToTarget();
						}
					}
				}
			}
		});
	}

	/**
	 * 获取网络数据
	 * 
	 * @param _context
	 * @param _url
	 * @param _params
	 * @param _handle
	 * @param _success
	 * 
	 */
	public static void executeHttpDataNoData(final Context _context,
			final String _url, final List<NameValuePair> _params, final Handle _handle, final int _success, final int _fail) {
		Pool.getPool().execute(new Runnable() {
			@Override
			public void run() {
				String err = "";
				if (HttpRequestHelper.getUserId(_context)) {
					ResponeResult responeResult = new ResponeResult();
					try {
						HttpEntity entity = HttpHelper.getInfoByPost(_url, _params, _context);
						String _result = HttpHelper.entity2String(entity);
						JSONObject jsonResult = new JSONObject(_result);
						JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
						int code = result.getInt(HttpCommonInfo.RESULT_CODE);
						String msg = result.getString(HttpCommonInfo.RESULT_MSG);
						responeResult.setCode(code);
						responeResult.setMsg(msg);
						if (code == 0) {
							_handle.obtainMessage(_success, responeResult).sendToTarget();
						} else {
							_handle.obtainMessage(_fail, msg).sendToTarget();
						}
					} catch (ClientProtocolException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (IOException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (JSONException e) {
						e.printStackTrace();
						responeResult = null;
					} finally {
						if (!TextUtils.isEmpty(err)) {
							_handle.obtainMessage(_fail, err).sendToTarget();
						}
					}
				}
			}
		});
	}

	public static void executeHttpDataUnThread(final Context _context,
			final String _url, final List<NameValuePair> _params,final Handle _handle, final int _success, final int _fail) {
		if (HttpRequestHelper.getUserId(_context)) {
			ResponeResult responeResult = new ResponeResult();
			try {
				HttpEntity entity = HttpHelper.getInfoByPost(_url, _params, _context);
				String _result = HttpHelper.entity2String(entity);
				JSONObject jsonResult = new JSONObject(_result);
				JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
				int code = result.getInt(HttpCommonInfo.RESULT_CODE);
				String msg = result.getString(HttpCommonInfo.RESULT_MSG);
				responeResult.setCode(code);
				responeResult.setMsg(msg);
				if (code == 0) {
					responeResult.setData(jsonResult.getString(HttpCommonInfo.DATA));
					_handle.obtainMessage(_success, responeResult).sendToTarget();
				} else {
					_handle.obtainMessage(_fail, msg).sendToTarget();
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
				responeResult = null;
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				responeResult = null;
			} catch (IOException e) {
				e.printStackTrace();
				responeResult = null;
			} catch (JSONException e) {
				e.printStackTrace();
				responeResult = null;
			} finally {
				if (responeResult == null) {
					_handle.obtainMessage(_fail,_context.getResources().getString(R.string.connect_err_io)).sendToTarget();
				}
			}
		}
	}

	public static void executeHttpData2(final Context _context,
			final String _url, final List<NameValuePair> _params,final Handle _handle, final int _success, final int _fail) {
		Pool.getPool().execute(new Runnable() {
			@Override
			public void run() {
				if (HttpRequestHelper.getUserId(_context)) {
					ResponeResult responeResult = new ResponeResult();
					try {
						HttpEntity entity = HttpHelper.getInfoByPost(_url, _params, _context);
						String _result = HttpHelper.entity2String(entity);
						JSONObject jsonResult = new JSONObject(_result);
						JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
						int code = result.getInt(HttpCommonInfo.RESULT_CODE);
						String msg = result.getString(HttpCommonInfo.RESULT_MSG);
						responeResult.setCode(code);
						responeResult.setMsg(msg);
						if (code == 0) {
							_handle.obtainMessage(_success, responeResult).sendToTarget();
						} else {
							_handle.obtainMessage(-100, msg).sendToTarget();
						}
					} catch (ClientProtocolException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (IOException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (JSONException e) {
						e.printStackTrace();
						responeResult = null;
					} finally {
						if (responeResult == null) {
							_handle.obtainMessage(_fail,_context.getResources().getString(R.string.connect_err_io)).sendToTarget();
						}
					}
				}
			}
		});
	}

	/**
	 * 获取首页banner列表
	 * 
	 * @param _url
	 * @param _params
	 * @param _context
	 * @param _handle
	 * @param tag
	 */
	public static void getBannerList(Context _context, Handle _handle) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(USERID, String.valueOf(SharedPreferencesUtils.getParam(_context, USERID, 0))));
		HttpHelper.executeHttpRequest(_context, "home_page_banner_list", BANNDE_LIST, params, _handle);
	}

	/**
	 * 添加作品收藏
	 * 
	 * @param _context
	 * @param _handle
	 */
	public static void AddCollect(final Context _context, final Handle _handle, final int bookId, final int collect_fail) {
		Pool.getPool().execute(new Runnable() {

			@Override
			public void run() {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair(USERID, String.valueOf(SharedPreferencesUtils.getParam(_context,USERID, 0))));
				params.add(new BasicNameValuePair(BOOKID, String.valueOf(bookId)));
				ResponeResult responeResult = new ResponeResult();
				try {
					HttpEntity entity = HttpHelper.getInfoByPost(HttpRequestHelper.BOOK_COLLECT_ADD, params,_context);
					String _result = HttpHelper.entity2String(entity);
					JSONObject jsonResult = new JSONObject(_result);
					JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
					int code = result.getInt(HttpCommonInfo.RESULT_CODE);
					String msg = result.getString(HttpCommonInfo.RESULT_MSG);
					responeResult.setCode(code);
					responeResult.setMsg(msg);
					if (code == 0) {
						responeResult.setData(bookId + "");
						Cache.cacheLocalIsFavor(bookId, 2);
						_handle.obtainMessage(COLLECT_SUCCESS, responeResult).sendToTarget();
					} else {
						_handle.sendEmptyMessage(-100);
					}
				} catch (ClientProtocolException e) {
					e.printStackTrace();
					responeResult = null;
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
					responeResult = null;
				} catch (IOException e) {
					e.printStackTrace();
					responeResult = null;
				} catch (JSONException e) {
					e.printStackTrace();
					responeResult = null;
				} finally {
					if (responeResult == null) {
						_handle.obtainMessage(collect_fail,_context.getResources().getString(R.string.connect_err_io)).sendToTarget();
					}
				}
			}
		});
	}
	
	/**
	 * 购买书
	 * @param _context
	 * @param _handle
	 * @param bookId
	 * @param collect_fail
	 */
	public static void buy(final Context _context, final Handler _handle, final int bookId, final int callback_success, final int callback_fail) {
		Pool.getPool().execute(new Runnable() {

			@Override
			public void run() {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair(USERID, String.valueOf(SharedPreferencesUtils.getParam(_context,USERID, 0))));
				if (bookId > 0) {
					params.add(new BasicNameValuePair(BOOKID, String.valueOf(bookId)));
				}
				String sessionId = MyUserInfo.getInstance().getUserInfoBean().getSessionId();
				params.add(new BasicNameValuePair("sessionId", TextUtils.isEmpty(sessionId) ? "" : sessionId));
				ResponeResult responeResult = new ResponeResult();
				try {
					HttpEntity entity = HttpHelper.getInfoByPost(bookId > 0 ? HttpRequestHelper.PAY_BUY_BOOK : HttpRequestHelper.PAY_BUY_YEAR, params,_context);
					String _result = HttpHelper.entity2String(entity);
					JSONObject jsonResult = new JSONObject(_result);
					JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
					int code = result.getInt(HttpCommonInfo.RESULT_CODE);
					String msg = result.getString(HttpCommonInfo.RESULT_MSG);
					responeResult.setCode(code);
					responeResult.setMsg(msg);
					responeResult.setData(jsonResult.optString(HttpCommonInfo.DATA,""));
					_handle.obtainMessage(callback_success, responeResult).sendToTarget();
				} catch (ClientProtocolException e) {
					e.printStackTrace();
					responeResult = null;
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
					responeResult = null;
				} catch (IOException e) {
					e.printStackTrace();
					responeResult = null;
				} catch (JSONException e) {
					e.printStackTrace();
					responeResult = null;
				} finally {
					if (responeResult == null) {
						_handle.obtainMessage(callback_fail,_context.getResources().getString(R.string.connect_err_io)).sendToTarget();
					}
				}
			}
		});
	}
	
	/**
	 * 购买书单
	 * @param _context
	 * @param _handle
	 * @param bookId
	 * @param collect_fail
	 */
	public static void buyBookList(final Context _context, final Handler _handle, final int bookListId, final int callback_success, final int callback_fail) {
		Pool.getPool().execute(new Runnable() {

			@Override
			public void run() {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair(USERID, String.valueOf(SharedPreferencesUtils.getParam(_context,USERID, 0))));
				params.add(new BasicNameValuePair("listId", String.valueOf(bookListId)));
				String sessionId = MyUserInfo.getInstance().getUserInfoBean().getSessionId();
				params.add(new BasicNameValuePair("sessionId", TextUtils.isEmpty(sessionId) ? "" : sessionId));
				ResponeResult responeResult = new ResponeResult();
				try {
					HttpEntity entity = HttpHelper.getInfoByPost(HttpRequestHelper.PAY_BUY_BOOK_LIST, params,_context);
					String _result = HttpHelper.entity2String(entity);
					JSONObject jsonResult = new JSONObject(_result);
					JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
					int code = result.getInt(HttpCommonInfo.RESULT_CODE);
					String msg = result.getString(HttpCommonInfo.RESULT_MSG);
					responeResult.setCode(code);
					responeResult.setMsg(msg);
					responeResult.setData(jsonResult.optString(HttpCommonInfo.DATA,""));
					_handle.obtainMessage(callback_success, responeResult).sendToTarget();
				} catch (ClientProtocolException e) {
					e.printStackTrace();
					responeResult = null;
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
					responeResult = null;
				} catch (IOException e) {
					e.printStackTrace();
					responeResult = null;
				} catch (JSONException e) {
					e.printStackTrace();
					responeResult = null;
				} finally {
					if (responeResult == null) {
						_handle.obtainMessage(callback_fail,_context.getResources().getString(R.string.connect_err_io)).sendToTarget();
					}
				}
			}
		});
	}
	

	public static void sendListenStatus(final Context _context, final String _url, final List<NameValuePair> _params,final Handle _handle, final int _success, final int _fail,final ListenStatusBean listenStatusBean) {

		Pool.getPool().execute(new Runnable() {
			@Override
			public void run() {
				String err = "";
				if (HttpRequestHelper.getUserId(_context)) {
					ResponeResult responeResult = new ResponeResult();
					try {
						HttpEntity entity = HttpHelper.getInfoByPost(_url, _params, _context);
						String _result = HttpHelper.entity2String(entity);
						JSONObject jsonResult = new JSONObject(_result);
						JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
						int code = result.getInt(HttpCommonInfo.RESULT_CODE);
						String msg = result.getString(HttpCommonInfo.RESULT_MSG);
						responeResult.setCode(code);
						responeResult.setMsg(msg);
						if (code == 0) {
							responeResult.setData(jsonResult.getString(HttpCommonInfo.DATA));
							_handle.obtainMessage(_success,listenStatusBean.getBookId(), 0,responeResult).sendToTarget();
							JSONObject jsonObject = new JSONObject(jsonResult.getString(HttpCommonInfo.DATA));
							int isChange = jsonObject.getInt(HttpRequestHelper.BOOKNUM_ISCHANGED); // 1否2是
							if (isChange == 2) {
								try {
									BookBean bookBean = StaticReference.mutilDBSupport.getDao(BookBean.class).queryForId(listenStatusBean.getBookId());
									if (bookBean != null && bookBean.getIsRead() != 2) {
										bookBean.setIsRead(2);
										bookBean.update();
										if (TemporaryVariable.downLoadedList.contains(bookBean.getBookId())) { // 删除了再添加进去
											CacheTypeBean cacheTypeBean = StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).queryBuilder().where().eq("bookId",bookBean.getBookId()).and().eq("type", 1).queryForFirst();
											if (cacheTypeBean != null) {
												cacheTypeBean.delete();
												CacheTypeBean cacheTypeBean2 = new CacheTypeBean();
												cacheTypeBean2.setBookId(bookBean.getBookId());
												cacheTypeBean2.setType(1);
												StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).create(cacheTypeBean2);
											}
										}
										EventBus.getEventBus("m_listen_statue_change").post(new DownLoadRefreshEvent());
									}
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						} else {
							_handle.obtainMessage(_fail, msg).sendToTarget();
						}
					} catch (ClientProtocolException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (IOException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (JSONException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (NullPointerException e) {
						e.printStackTrace();
						responeResult = null;
					} finally {
						if (!TextUtils.isEmpty(err)) {
							try {
								StaticReference.mutilDBSupport.getDao(ListenStatusBean.class).create(listenStatusBean);
							} catch (SQLException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		});
	}

	public static void sendBookReadStatus(final Context _context,final String _url, final List<NameValuePair> _params,final Handle _handle, final int _success, final int _fail,final int bookId) {

		Pool.getPool().execute(new Runnable() {
			@Override
			public void run() {
				String err = "";
				if (HttpRequestHelper.getUserId(_context)) {
					ResponeResult responeResult = new ResponeResult();
					try {
						HttpEntity entity = HttpHelper.getInfoByPost(_url, _params, _context);
						String _result = HttpHelper.entity2String(entity);
						JSONObject jsonResult = new JSONObject(_result);
						JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
						int code = result.getInt(HttpCommonInfo.RESULT_CODE);
						String msg = result.getString(HttpCommonInfo.RESULT_MSG);
						responeResult.setCode(code);
						responeResult.setMsg(msg);
						if (code == 0) {
							responeResult.setData(jsonResult.getString(HttpCommonInfo.DATA));
							_handle.obtainMessage(_success, responeResult).sendToTarget();
							JSONObject jsonObject = new JSONObject(jsonResult.getString(HttpCommonInfo.DATA));
							int isChange = jsonObject.getInt(HttpRequestHelper.BOOKNUM_ISCHANGED); // 1否2是
							if (isChange == 2) {
								try {
									BookBean bookBean = StaticReference.mutilDBSupport.getDao(BookBean.class).queryForId(bookId);
									if (bookBean != null && bookBean.getIsRead() != 2) {
										bookBean.setIsRead(2);
										bookBean.update();
										if (TemporaryVariable.downLoadedList.contains(bookBean.getBookId())) { // 删除了再添加进去
											CacheTypeBean cacheTypeBean = StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).queryBuilder().where().eq("bookId",bookBean.getBookId()).and().eq("type", 1).queryForFirst();
											if (cacheTypeBean != null) {
												cacheTypeBean.delete();
												CacheTypeBean cacheTypeBean2 = new CacheTypeBean();
												cacheTypeBean2.setBookId(bookBean.getBookId());
												cacheTypeBean2.setType(1);
												StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).create(cacheTypeBean2);
											}
										}
										EventBus.getEventBus("m_listen_statue_change").post(new DownLoadRefreshEvent());
									}
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						} else {
							_handle.obtainMessage(_fail, msg).sendToTarget();
						}
					} catch (ClientProtocolException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (IOException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (JSONException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (NullPointerException e) {
						e.printStackTrace();
						responeResult = null;
					} finally {
						if (!TextUtils.isEmpty(err)) {
							try {
								ListenStatusBean listenStatusBean = new ListenStatusBean();
								listenStatusBean.setUserId((int) SharedPreferencesUtils.getParam(_context, USERID, 0));
								listenStatusBean.setBookId(bookId);
								StaticReference.mutilDBSupport.getDao(ListenStatusBean.class).create(listenStatusBean);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		});
	}

	/**
	 * 添加作品收藏
	 * 
	 * @param _context
	 * @param _handle
	 */
	public static void cancelCollect(final Context _context,
			final Handle _handle, final int bookId) {
		Pool.getPool().execute(new Runnable() {

			@Override
			public void run() {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair(USERID, String.valueOf(SharedPreferencesUtils.getParam(_context,USERID, 0))));
				params.add(new BasicNameValuePair(BOOKIDS, String.valueOf(bookId)));
				ResponeResult responeResult = new ResponeResult();
				try {
					HttpEntity entity = HttpHelper.getInfoByPost(HttpRequestHelper.CANCEL_COLLECT_BOOK, params,_context);
					String _result = HttpHelper.entity2String(entity);
					JSONObject jsonResult = new JSONObject(_result);
					JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
					int code = result.getInt(HttpCommonInfo.RESULT_CODE);
					String msg = result.getString(HttpCommonInfo.RESULT_MSG);
					responeResult.setCode(code);
					responeResult.setMsg(msg);
					if (code == 0) {
						responeResult.setData(bookId + "");
						Cache.cacheLocalIsFavor(bookId, 1);
						_handle.obtainMessage(CANCEL_COLLECT_SUCCESS, responeResult).sendToTarget();
					} else {
						_handle.sendEmptyMessage(-100);
					}
				} catch (ClientProtocolException e) {
					e.printStackTrace();
					responeResult = null;
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
					responeResult = null;
				} catch (IOException e) {
					e.printStackTrace();
					responeResult = null;
				} catch (JSONException e) {
					e.printStackTrace();
					responeResult = null;
				}
			}
		});
	}

	public static boolean getUserId(Context context) {
		int userId = (int) SharedPreferencesUtils.getParam(context, USERID, 0);
		if (userId == 0) {
			String imsi = CommonMethod.getUUID(context);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair(IMSI_KEY, imsi));
			try {
				HttpEntity entity = HttpHelper.getInfoByPost(HttpCommonInfo.GET_USER_ID, params, context);
				String result = HttpHelper.entity2String(entity);
				JSONObject resultJsonObject = new JSONObject(result);
				JSONObject resultDetail = resultJsonObject.getJSONObject(HttpCommonInfo.RESULT);
				int resultCode = resultDetail.getInt(HttpCommonInfo.RESULT_CODE);
				if (resultCode == 0) {
					JSONObject data = resultJsonObject.getJSONObject(HttpCommonInfo.DATA);
					userId = data.getInt(HttpCommonInfo.USER_ID);
					SharedPreferencesUtils.setParam(context, USERID, userId);
					return true;
				} else {
					return false;
				}
			} catch (Exception e) {
				// Log.d("error", e.getMessage());
				return false;
			}
		} else {
			return true;
		}
	}

	/**
	 * 获取主播列表
	 * 
	 * @param context
	 * @return
	 */
	public static boolean getAnchorList(Context context, int bookId, Handle handle) {
		try {
			List<AnchorBookBean> anchorList = StaticReference.mutilDBSupport.getDao(AnchorBookBean.class).queryBuilder().where().eq("bookId", bookId).query();
			ArrayList<AnchorBean> m_anchorList = new ArrayList<AnchorBean>();
			if (anchorList != null && anchorList.size() > 0) {
				for (AnchorBookBean anchorBookBean : anchorList) {
					AnchorBean m_anchor_list = StaticReference.mutilDBSupport.getDao(AnchorBean.class).queryForId(anchorBookBean.getAnchorId());
					m_anchorList.add(m_anchor_list);
				}
				if (m_anchorList != null && m_anchorList.size() > 0) {
					return true;
				}
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("bookId", String.valueOf(bookId)));
		try {
			HttpEntity entity = HttpHelper.getInfoByPost(BOOK_ANCHOR_LIST,params, context);
			String result = HttpHelper.entity2String(entity);
			JSONObject resultJsonObject = new JSONObject(result);
			JSONObject resultDetail = resultJsonObject.getJSONObject(HttpCommonInfo.RESULT);
			int resultCode = resultDetail.getInt(HttpCommonInfo.RESULT_CODE);
			if (resultCode == 0) {
				JSONArray data = resultJsonObject.getJSONArray(HttpCommonInfo.DATA);
				List<AnchorBean> list = new Gson().fromJson(data.toString(),new TypeToken<List<AnchorBean>>() {}.getType());
				if (list != null && list.size() > 0) {
					for (AnchorBean bean : list) {
						try {
							StaticReference.mutilDBSupport.getDao(AnchorBean.class).createOrUpdate(bean);
							AnchorBookBean anchorBookBean = StaticReference.mutilDBSupport.getDao(AnchorBookBean.class).queryBuilder().where().eq("bookId", bookId).and().eq("anchorId", bean.getId()).queryForFirst();
							if (anchorBookBean == null) {
								anchorBookBean = new AnchorBookBean();
								anchorBookBean.setAnchorId(bean.getId());
								anchorBookBean.setBookId(bookId);
								StaticReference.mutilDBSupport.getDao(AnchorBookBean.class).createOrUpdate(anchorBookBean);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
					handle.obtainMessage(100, list).sendToTarget();
				}
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 上传收听状态
	 * 
	 * @param _context
	 * @param _handle
	 */
	public static void sendListenStatue(Context _context, Handle _handle, int bookId, int sectionId, AMapLocation startLocation, AMapLocation endLocation, int listenTime) {
		ListenStatusBean listenStatusBean = new ListenStatusBean();
		int style = (int) SharedPreferencesUtils.getParam(_context,"chapter_type", 1);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(USERID, String.valueOf(SharedPreferencesUtils.getParam(_context, USERID, 0))));
		params.add(new BasicNameValuePair(BOOKID, String.valueOf(bookId)));
		params.add(new BasicNameValuePair(SECTIONID, String.valueOf(sectionId)));

		listenStatusBean.setBookId(bookId);
		listenStatusBean.setSelectId(sectionId);
		listenStatusBean.setUserId((int) SharedPreferencesUtils.getParam(_context, USERID, 0));

		if (startLocation == null) {
			params.add(new BasicNameValuePair("lng1", String.valueOf(0)));
			params.add(new BasicNameValuePair("lat1", String.valueOf(0)));
			listenStatusBean.setLng1(0);
			listenStatusBean.setLat1(0);
		} else {
			params.add(new BasicNameValuePair("lng1", String.valueOf(startLocation.getLongitude())));
			params.add(new BasicNameValuePair("lat1", String.valueOf(startLocation.getLatitude())));
			listenStatusBean.setLng1(startLocation.getLongitude());
			listenStatusBean.setLat1(startLocation.getLatitude());
		}
		if (endLocation == null) {
			params.add(new BasicNameValuePair("lng2", String.valueOf(0)));
			params.add(new BasicNameValuePair("lat2", String.valueOf(0)));
			listenStatusBean.setLng2(0);
			listenStatusBean.setLat2(0);
		} else {
			params.add(new BasicNameValuePair("lng2", String.valueOf(endLocation.getLongitude())));
			params.add(new BasicNameValuePair("lat2", String.valueOf(endLocation.getLatitude())));
			listenStatusBean.setLng2(endLocation.getLongitude());
			listenStatusBean.setLat2(endLocation.getLatitude());
		}
		params.add(new BasicNameValuePair("listenTime", String.valueOf(listenTime)));
		params.add(new BasicNameValuePair("style", String.valueOf(style)));
		listenStatusBean.setListenTime(listenTime);
		listenStatusBean.setStyle(style);
		HttpRequestHelper.sendListenStatus(_context, BOOK_LISTEN, params,_handle, SEND_LISTEN_STATUS_SUCCESS, SEND_LISTEN_STATUS_FAIL,listenStatusBean);
	}

	/**
	 * qq或者微信登录
	 * 
	 * @param context
	 * @param weixin
	 * @param fullName
	 * @param faceUrl
	 * @param isWeixin
	 * @param handler
	 */
	public static void login2(Context context, String weixin, String fullName,
			String faceUrl, boolean isWeixin, Handle handler) {
		try {
			int type = 0;
			ResponeResult responeResult = new ResponeResult();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair(HttpCommonInfo.USER_ID, String.valueOf(SharedPreferencesUtils.getParam(context, USERID, 0))));
			params.add(new BasicNameValuePair("fullName", fullName));
			params.add(new BasicNameValuePair("faceUrl", faceUrl));
			String url = "";
			if (isWeixin) {
				params.add(new BasicNameValuePair("weixin", weixin));
				url = wexin_login;
				type = 1;
			} else {
				params.add(new BasicNameValuePair("qq", weixin));
				url = qq_login;
				type = 2;
			}
			HttpEntity entity = HttpHelper.getInfoByPost(url, params, context);
			String result = HttpHelper.entity2String(entity);
			JSONObject resultJsonObject = new JSONObject(result);
			JSONObject resultDetail = resultJsonObject.getJSONObject(HttpCommonInfo.RESULT);
			int resultCode = resultDetail.getInt(HttpCommonInfo.RESULT_CODE);
			String msg = resultDetail.getString(HttpCommonInfo.RESULT_MSG);
			responeResult.setCode(resultCode);
			responeResult.setMsg(msg);
			if (resultCode == 0) {
				UserInfoBean userInfoBean = new Gson().fromJson(resultJsonObject.getString(HttpCommonInfo.DATA),new TypeToken<UserInfoBean>() {}.getType());
				if (userInfoBean != null) {
					EventBus.getEventBus("login_success_alise_tag").post(userInfoBean);
				}
				SharedPreferencesUtils.setParam(context, "loginType", type);
				responeResult.setData(resultJsonObject.getString(HttpCommonInfo.DATA));
				Message msga = handler.obtainMessage(PUBLIC_LOGIN_SUCCESS,responeResult);
				handler.sendMessage(msga);
			} else {
				String err = resultDetail.getString(HttpCommonInfo.RESULT_MSG);
				Message msgb = handler.obtainMessage(PUBLIC_LOGIN_FAIL);
				msgb.obj = err;
				handler.sendMessage(msgb);
			}
		} catch (Exception e) {
			e.printStackTrace();
			String err2 = context.getString(R.string.connect_err_io);
			Message msgb = handler.obtainMessage(PUBLIC_LOGIN_FAIL);
			msgb.obj = err2;
			handler.sendMessage(msgb);
		}
	}

	public static void logout(Context context, String sessionId, Handle handle) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(IMSI_KEY, CommonMethod.getUUID(context)));
		params.add(new BasicNameValuePair("sessionId", sessionId));
		HttpRequestHelper.executeHttpData(context, LOG_OUT, params, handle, GETDATA_SUCCESS);
	}

	/**
	 * 添加分享记录
	 * 
	 * @param context
	 * @param shareType
	 * @param shareTag
	 * @param shareId
	 */
	public static void addUserShare(Context _context, int shareType, int shareTag, int shareId) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(HttpCommonInfo.USER_ID, String.valueOf(SharedPreferencesUtils.getParam(_context, USERID, 0))));
		params.add(new BasicNameValuePair("shareType", String.valueOf(shareType)));
		params.add(new BasicNameValuePair("shareTag", String.valueOf(shareTag)));
		if (shareId > 0) {
			params.add(new BasicNameValuePair("shareId", String.valueOf(shareId)));
		}
		executeHttpDataNoData(_context, USER_SHARE_ADD, params, new Handle(_context), -991, -992);
	}

	/**
	 * 用户bind手机号码
	 * 
	 * @param _context
	 * @param userTel
	 * @param pwd
	 * @param handle
	 */
	public static void BindTel(Context _context, String userTel, String pwd, Handle handle, int success) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(HttpRequestHelper.USERID, String.valueOf(SharedPreferencesUtils.getParam(_context, HttpRequestHelper.USERID, 0))));
		params.add(new BasicNameValuePair("tel", userTel));
		if (!TextUtils.isEmpty(pwd)) {
			params.add(new BasicNameValuePair("pwd", pwd));
		}
		params.add(new BasicNameValuePair("noPwd", String.valueOf(1)));
		executeHttpData(_context, USER_BIND_TEL, params, handle, success, HttpRequestHelper.GETDATA_FAIL);
	}

	/**
	 * 添加视频播放量
	 * 
	 * @param _context
	 * @param videoId
	 */
	public static void addVideoPlayNum(Context _context, int videoId) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(HttpRequestHelper.USERID, String.valueOf(SharedPreferencesUtils.getParam(_context,HttpRequestHelper.USERID, 0))));
		params.add(new BasicNameValuePair("videoId", String.valueOf(videoId)));
		executeHttpDataNoData(_context, HOF_LOOK, params, new Handle(_context),-123, -124);
	}

	public static void getScreenImage(Context _context, Handle handle) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(HttpRequestHelper.USERID, String.valueOf(SharedPreferencesUtils.getParam(_context,HttpRequestHelper.USERID, 0))));
		executeHttpData(_context, BOOT_SCREEN, params, handle, -123, -124);
	}

	/**
	 * 主播点赞
	 * 
	 * @param _context
	 * @param anchorId
	 * @param bookId
	 * @param handle
	 */
	public static void addAnchorFavorNum(final Context _context,
			final List<NameValuePair> _params, final Handle _handle,
			final int _success, final int _fail, final String anchorId) {

		// executeHttpData(_context, ANCHOR_FAVOUR, params, handle,
		// PlayFragment.ANCHOR_FAVOR_SUCCESS, PlayFragment.ANCHOR_FAVOR_FAIL);

		Pool.getPool().execute(new Runnable() {
			@Override
			public void run() {
				String err = "";
				if (HttpRequestHelper.getUserId(_context)) {
					ResponeResult responeResult = new ResponeResult();
					try {
						HttpEntity entity = HttpHelper.getInfoByPost(ANCHOR_FAVOUR, _params, _context);
						String _result = HttpHelper.entity2String(entity);
						JSONObject jsonResult = new JSONObject(_result);
						JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
						int code = result.getInt(HttpCommonInfo.RESULT_CODE);
						String msg = result.getString(HttpCommonInfo.RESULT_MSG);
						responeResult.setCode(code);
						responeResult.setMsg(msg);
						if (code == 0) {
							responeResult.setData(anchorId + "");
							_handle.obtainMessage(_success, responeResult).sendToTarget();
						} else {
							_handle.obtainMessage(_fail, msg).sendToTarget();
						}
					} catch (ClientProtocolException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (IOException e) {
						e.printStackTrace();
						responeResult = null;
						err = _context.getString(R.string.connect_err_io);
					} catch (JSONException e) {
						e.printStackTrace();
						responeResult = null;
					} finally {
						if (!TextUtils.isEmpty(err)) {
							_handle.obtainMessage(_fail, err).sendToTarget();
						}
					}
				}
			}
		});
	}

	public static void getGuessYouLike(final Context context,final String _url, final List<NameValuePair> _params,final Handle handle, final int GETDATA_SUCCESS,final int GETDAATE_FAIL) {
		Pool.getPool().execute(new Runnable() {
			@Override
			public void run() {
				if (HttpRequestHelper.getUserId(context)) {
					ResponeResult responeResult = new ResponeResult();
					try {
						HttpEntity entity = HttpHelper.getInfoByPost(_url,_params, context);
						String _result = HttpHelper.entity2String(entity);
						JSONObject jsonResult = new JSONObject(_result);
						JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
						int code = result.getInt(HttpCommonInfo.RESULT_CODE);
						String msg = result.getString(HttpCommonInfo.RESULT_MSG);
						responeResult.setCode(code);
						responeResult.setMsg(msg);
						if (code == 0) {
							responeResult.setData(jsonResult.getString(HttpCommonInfo.DATA));
							handle.obtainMessage(GETDATA_SUCCESS, responeResult).sendToTarget();
						} else {
							handle.obtainMessage(GETDAATE_FAIL, msg).sendToTarget();
						}
					} catch (ClientProtocolException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (IOException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (JSONException e) {
						e.printStackTrace();
						responeResult = null;
					} catch (Exception e) {
						e.printStackTrace();
						responeResult = null;
					} finally {
						if (responeResult == null) {
							handle.obtainMessage(GETDAATE_FAIL,context.getResources().getString(R.string.connect_err_io)).sendToTarget();
						}
					}
				}
			}
		});
	}

	// 同步收听纪录
	public static void synchronousListenStatus(final Context _context, final Handler _handler) {
		try {
			List<ListenStatusBean> listenStatusList = StaticReference.mutilDBSupport.getDao(ListenStatusBean.class).queryForAll();
			if (listenStatusList != null && listenStatusList.size() > 0) {
				for (ListenStatusBean listenStatusBean : listenStatusList) {
					if (listenStatusBean.getListenTime() > 0) {
						List<NameValuePair> params = new ArrayList<NameValuePair>();
						params.add(new BasicNameValuePair(HttpRequestHelper.USERID, String.valueOf(listenStatusBean.getUserId())));
						params.add(new BasicNameValuePair(HttpRequestHelper.BOOKID, String.valueOf(listenStatusBean.getBookId())));
						params.add(new BasicNameValuePair(HttpRequestHelper.SECTIONID,String.valueOf(listenStatusBean.getSelectId())));
						params.add(new BasicNameValuePair("lng1", String.valueOf(listenStatusBean.getLng1())));
						params.add(new BasicNameValuePair("lat1", String.valueOf(listenStatusBean.getLat1())));
						params.add(new BasicNameValuePair("lng2", String.valueOf(listenStatusBean.getLng2())));
						params.add(new BasicNameValuePair("lat2", String.valueOf(listenStatusBean.getLat2())));
						params.add(new BasicNameValuePair("listenTime", String.valueOf(listenStatusBean.getListenTime())));
						params.add(new BasicNameValuePair("style", String.valueOf(listenStatusBean.getStyle())));
						if (HttpRequestHelper.getUserId(_context)) {
							ResponeResult responeResult = new ResponeResult();
							try {
								HttpEntity entity = HttpHelper.getInfoByPost(HttpRequestHelper.BOOK_LISTEN, params,_context);
								String _result = HttpHelper.entity2String(entity);
								JSONObject jsonResult = new JSONObject(_result);
								JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
								int code = result.getInt(HttpCommonInfo.RESULT_CODE);
								String msg = result.getString(HttpCommonInfo.RESULT_MSG);
								responeResult.setCode(code);
								responeResult.setMsg(msg);
								if (code == 0) {
									StaticReference.mutilDBSupport.getDao(ListenStatusBean.class).deleteById(listenStatusBean.getId());
									responeResult.setData(jsonResult.getString(HttpCommonInfo.DATA));
									_handler.obtainMessage(11,listenStatusBean.getBookId(), 0,responeResult).sendToTarget();

									JSONObject jsonObject = new JSONObject(jsonResult.getString(HttpCommonInfo.DATA));
									int isChange = jsonObject.getInt(HttpRequestHelper.BOOKNUM_ISCHANGED); // 1否2是
									if (isChange == 2) {
										try {
											BookBean bookBean = StaticReference.mutilDBSupport.getDao(BookBean.class).queryForId(listenStatusBean.getBookId());
											if (bookBean != null && bookBean.getIsRead() != 2) {
												bookBean.setIsRead(2);
												bookBean.update();
											}
										} catch (Exception e) {
											e.printStackTrace();
										}
									}
								}
							} catch (Exception e) {
								e.printStackTrace();
								responeResult = null;
							}
						}
					} else {
						List<NameValuePair> params = new ArrayList<NameValuePair>();
						params.add(new BasicNameValuePair(HttpRequestHelper.USERID, String.valueOf(listenStatusBean.getUserId())));
						params.add(new BasicNameValuePair(HttpRequestHelper.BOOKID, String.valueOf(listenStatusBean.getBookId())));
						if (HttpRequestHelper.getUserId(_context)) {
							ResponeResult responeResult = new ResponeResult();
							try {
								HttpEntity entity = HttpHelper.getInfoByPost(HttpRequestHelper.BOOK_READ, params,_context);
								String _result = HttpHelper.entity2String(entity);
								JSONObject jsonResult = new JSONObject(_result);
								JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
								int code = result.getInt(HttpCommonInfo.RESULT_CODE);
								String msg = result.getString(HttpCommonInfo.RESULT_MSG);
								responeResult.setCode(code);
								responeResult.setMsg(msg);
								if (code == 0) {
									StaticReference.mutilDBSupport.getDao(ListenStatusBean.class).deleteById(listenStatusBean.getId());
									responeResult.setData(jsonResult.getString(HttpCommonInfo.DATA));
									_handler.obtainMessage(12, responeResult).sendToTarget();

									JSONObject jsonObject = new JSONObject(jsonResult.getString(HttpCommonInfo.DATA));
									int isChange = jsonObject.getInt(HttpRequestHelper.BOOKNUM_ISCHANGED); // 1否2是
									if (isChange == 2) {
										try {
											BookBean bookBean = StaticReference.mutilDBSupport.getDao(BookBean.class).queryForId(listenStatusBean.getBookId());
											if (bookBean != null && bookBean.getIsRead() != 2) {
												bookBean.setIsRead(2);
												bookBean.update();
											}
										} catch (Exception e) {
											e.printStackTrace();
										}
									}
								}
							} catch (Exception e) {
								e.printStackTrace();
								responeResult = null;
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取短信验证码
	 * 
	 * @param tel
	 * @param type
	 */
	public static void getVerCode(Context context, Handle handle, String tel,
			int type, int success, int fail) {
		String err = "";
		int isOK = 1;
		try {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair(HttpCommonInfo.REQ_CODE_CODETYPE,String.valueOf(type)));
			params.add(new BasicNameValuePair(HttpCommonInfo.REQ_CODE_TEL, tel));
			HttpEntity entity = HttpHelper.getInfoByPost(HttpRequestHelper.CODE, params, context);
			String result = HttpHelper.entity2String(entity);
			JSONObject jObject = new JSONObject(parseResultJson(result));
			int resultCode = jObject.getInt(HttpCommonInfo.RESULT_CODE);
			if (resultCode == 0) {
				isOK = 0;
			} else {
				isOK = 1;
				err = jObject.getString(HttpCommonInfo.RESULT_MSG);
			}
		} catch (ClientProtocolException e) {
			err = context.getResources().getString(R.string.connect_err_unkonw);
			isOK = 1;
		} catch (IOException e) {
			err = context.getResources().getString(R.string.connect_err_io);
			isOK = 1;
		} catch (Exception e) {
			err = context.getResources().getString(R.string.connect_err_unkonw);
			isOK = 1;
		} finally {
			switch (isOK) {
			case 0:
				handle.sendEmptyMessage(success);
				break;
			case 1:
				handle.obtainMessage(fail, err).sendToTarget();
				break;
			}
		}
	}

	public static String parseResultJson(String result) throws JSONException {
		JSONObject jObject = new JSONObject(result);
		result = jObject.getString(HttpCommonInfo.RESULT);
		return result;
	}
}
